package com.project.estate.building.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.estate.building.service.BuildingService;
import com.project.estate.commons.bean.Building;
import com.project.estate.commons.bean.Car;
import com.project.estate.commons.config.JsonResult;
import com.project.estate.commons.config.ResultTool;
import com.project.estate.commons.handler.*;
import com.project.estate.commons.mapper.BuildingMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Resource
    private BuildingMapper buildingMapper;


    @Override
    public JsonResult addBuilding(Building building) {
        boolean save = save(building);
        if (!save) {
            throw new CarAddException("添加建筑失败！");
        }
        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteBuilding(String comId) {
        UpdateWrapper<Building> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);
        updateWrapper.set("delete_status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new BuildingDeleteException("删除失败");
        }
        return ResultTool.success("success");
    }

    //修改
    @Override
    public JsonResult updateBuilding(String comId, Building building) {
        UpdateWrapper<Building> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);
        updateWrapper.set("name",building.getName());
        updateWrapper.set("description",building.getDescription());
        updateWrapper.set("community_name",building.getCommunityName());
        updateWrapper.set("total_households",building.getTotalHouseholds());
        boolean update = update(updateWrapper);
        if (!update) {
            throw new BuildingUpdateException("修改失败");
        }
        return ResultTool.success("success");
    }

    //查询
    @Override
    public JsonResult findBuilding(String comId) {
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", comId);
        List<Building> list = list(queryWrapper);
        if (list.isEmpty()) {
            throw new BuildingFindException("未查询到");
        }
        return ResultTool.success(list);
    }
}
